package com.gxuwz.restaurant.shiro.realms;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gxuwz.restaurant.entity.User;
import com.gxuwz.restaurant.mapper.UserMapper;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import javax.annotation.Resource;
import java.util.List;

//自定义realm

public class CustomerRealm extends AuthorizingRealm {

    @Resource
    UserMapper userMapper;

    @Override//授权
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        System.out.println("调用授权,授权人的名字是："+principals);
         //获取用户身份信息
        String primaryPrincipal = (String)principals.getPrimaryPrincipal();
        //根据身分信息获取角色 和 权限信息
        QueryWrapper<User> queryWrapper =  new QueryWrapper<>();
        queryWrapper.eq("username",primaryPrincipal);
        User user = userMapper.selectOne(queryWrapper);
        System.out.println("角色是："+user.getRole());

        if(user.getRole().equals("系统管理员")){
            SimpleAuthorizationInfo simpleAuthenticationInfo = new SimpleAuthorizationInfo();
            simpleAuthenticationInfo.addRole("系统管理员");
            return simpleAuthenticationInfo;
        }else if(user.getRole().equals("店长")){
            SimpleAuthorizationInfo simpleAuthenticationInfo = new SimpleAuthorizationInfo();
            simpleAuthenticationInfo.addRole("店长");
            return simpleAuthenticationInfo;
        }else if(user.getRole().equals("店员")){
            SimpleAuthorizationInfo simpleAuthenticationInfo = new SimpleAuthorizationInfo();
            simpleAuthenticationInfo.addRole("店员");
            return simpleAuthenticationInfo;

        }else if (user.getRole().equals("厨师")){
            SimpleAuthorizationInfo simpleAuthenticationInfo = new SimpleAuthorizationInfo();
            simpleAuthenticationInfo.addRole("厨师");
            return simpleAuthenticationInfo;
        }else if(user.getRole().equals("保洁")){
            SimpleAuthorizationInfo simpleAuthenticationInfo = new SimpleAuthorizationInfo();
            simpleAuthenticationInfo.addRole("保洁");
            return simpleAuthenticationInfo;
        }else {
            return null;
        }

    }




    @Override//认证
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //根据身份信息
        String principal = (String) token.getPrincipal();
        QueryWrapper<User> queryWrapper =  new QueryWrapper<>();
        queryWrapper.eq("username",principal);
        User user1 = userMapper.selectOne(queryWrapper);

        if(!ObjectUtil.isAllEmpty(user1)){
            return new SimpleAuthenticationInfo(user1.getUsername(),user1.getPassword(), ByteSource.Util.bytes(user1.getSalt()),this.getName());

        }        return null;
    }
}
